# -*- coding: utf-8 -*-

import sys
import os
import time
import Queue

import config
import queryCore
import login
import jsonHander
import db
import figureThread
import figureImageThread

"""流程控制类"""
class FetchDirector():

    def __init__(self):
        self.login = False
        self.loginTime = None
        self.queryCore = queryCore.QueryCore(self)
        self.lg = login.Login(self)
        self.db = db.Database(self)
        self.queue = Queue.Queue()
        self.imageQueue = Queue.Queue()
        
        print '=============登陆Figure1中。。。==============='
        #self.lg.login()
        #if not self.lg.getLoginStatus():
        #    print '登陆Figure1失败'
        #    return 
        print '=============登陆Figure1成功！==============='
        
        
        print '=============获取Figure1分类中。。。 ==========='
        #category_json = self.queryCore.getCategoryData()
        #self.saveCategoryToFile(category_json)
        #self.saveCategoryToDB(category_json)
        print '=============获取Figure1分类完成==========='


        print '=============获取Figure1数据中。。。==========='
        

        #self.runFigureThread(self.queue)
        #jsons = self.queryCore.getHomeData(200, 83)
        
        #jsonHander.saveJsonFile(jsons, 'figure_83.json')
        
        print '=============获取Figure1完成。。。==========='
        
        print '=============下载Figure1图片。。。==========='
        '''
        list_dirs = os.walk('./json/figure')
        for root, dirs, files in list_dirs:
            #print files
            for f in files:
                print '解析文件' + str(os.path.join(root, f))
                imageFile = open(os.path.join(root, f))
                imageList = imageFile.read()
                #print imageList
                imageListData = jsonHander.json2orrdeedDict(imageList)
                for image in imageListData:
                    self.getImageIdUrl(image, self.imageQueue)
                print '当前队列大小：' + str(self.imageQueue.qsize())
                
            self.runFigureImageThread(self.imageQueue)
        '''
        print '=============下载Figure1图片完成==========='
        
        print '=============保存Figure1数据中。。。==========='
        
        self.getHomeDataInfo()
        
        print '=============保存Figure1完成。。。==========='
        
            
    """用户登陆，保存信息"""
    def submitLoginForm(self, loginData):
        loginStatus = self.queryCore.login(loginData)
        if loginStatus:
            self.login = True
            self.loginTime = time.time()
            return self.login
        else:
            return False

    '''保存Figure1分类到文件'''
    def saveCategoryToFile(self, json_str):
        jsonHander.saveJsonFile(json_str, 'category/category.json')

    '''保存Figure1分类到数据库'''
    def saveCategoryToDB(self, json_str):
        categorys = jsonHander.json2orrdeedDict(json_str)
        
        category_anatomy = categorys['Anatomy']
        category_specialty = categorys['Specialties']

        '''Anatomy分类'''
        anatomy = {}
        anatomy['name'] = category_anatomy['title']
        anatomy['title'] = category_anatomy['title']
        anatomy['image'] = ''
        anatomy['path'] = 0
        anatomy_subTypes = category_anatomy['subTypes']
        
        if (self.db.if_category_exist(category_anatomy['title'], category_anatomy['title'])):
            anatomy_id = self.db.get_category_by_title(category_anatomy['title'])['id']
        else:
            anatomy_id = self.db.insertCategoryByDict(anatomy, True)

        for (k, item) in anatomy_subTypes.items():
            subType = {}
            subType['name'] = item['title']
            subType['title'] = item['title']
            subType['image'] = ''
            subType['path'] = anatomy_id
            subType_categorys = item['categories']
            
            if (self.db.if_category_exist(item['title'], item['title'])):
                subType_id = self.db.get_category_by_title(item['title'])['id']
            else:
                subType_id = self.db.insertCategoryByDict(subType, True)
            
            for item_ in subType_categorys:
                print item_
                category = {}
                category['name'] = item_['title']
                category['title'] = item_['title']
                category['image'] = self.queryCore.saveCategoryImage(item_['title'], self.queryCore.getCategoryDisplaySrc(item_['id']))
                category['path'] = subType_id
                
                print '-------------------' + item_['title']
                
                if not self.db.if_category_exist(item_['title'], item_['title']):
                    self.db.insertCategoryByDict(category, True)

        '''Specialties分类'''
        specialty = {}
        specialty['name'] = category_specialty['title']
        specialty['title'] = category_specialty['title']
        specialty['image'] = ''
        specialty['path'] = 0
        specialty_categorys = category_specialty['subTypes']['none']['categories']
        
        if (self.db.if_category_exist(category_specialty['title'], category_specialty['title'])):
            specialty_id = self.db.get_category_by_title(category_specialty['title'])['id']
        else:
            specialty_id = self.db.insertCategoryByDict(specialty, True)

        for item_ in specialty_categorys:
            print item_
            category = {}
            category['name'] = item_['title']
            category['title'] = item_['title']
            category['image'] = self.queryCore.saveCategoryImage(item_['title'], self.queryCore.getCategoryDisplaySrc(item_['id']))
            category['path'] = specialty_id
                
            if not self.db.if_category_exist(item_['title'], item_['title']):
                self.db.insertCategoryByDict(category, True)

    def getHomeDataBySkip(self, skip):
        return self.queryCore.getHomeData(config.FIGURE1_PAGESIZE, skip)
        
    def saveHomeDataToFile(self, json_str, skip):
        jsonHander.saveJsonFile(json_str, 'figure/figure_' + str(skip) + '.json')
        
    def getHomeDataInfo(self):
        list_dirs = os.walk('./json/figure')
        for root, dirs, files in list_dirs:
            for f in files:
                print '解析文件' + str(os.path.join(root, f))
                jsonFile = open(os.path.join(root, f))
                jsonList = jsonFile.read()
                
                jsonListData = jsonHander.json2orrdeedDict(jsonList)
                for json_str in jsonListData:
                    #print json_str
                    self.saveHomeDataToDB(json_str)
                
    def saveHomeDataToDB(self, homeData):
        figure = {}   
        
        figure['imageID'] = homeData['imageID']
        figure['caption'] = homeData['caption']
        figure['author'] = homeData['author']['name'] 
        
        category_list = []
        for category_ in homeData['categoryVoteCollections']:
            category = category_['categoryName']
            category_list.append(category) 
        figure['categorys'] = ','.join(category_list)
        
        image_list = []
        if homeData.has_key('imageSet'):
            imageSet = homeData['imageSet']
            for imageItem in imageSet:
                image_list.append(imageItem['imageID'])
        else:
            image_list.append(homeData['urlToken'])
        figure['images'] = ','.join(image_list)
        
        commentID_list = []
        for comment_item in homeData['comments']:
            comment = {}
            comment['_id'] = comment_item['_id']
            comment['username'] = comment_item['username']
            if comment_item['author'] is None:
                comment['authorID'] = ''
                comment['authorSpecialtyName'] = ''
            else:
                comment['authorID'] = comment_item['author']['_id']
                if comment_item['author'].has_key('specialtyName'):
                    comment['authorSpecialtyName'] = comment_item['author']['specialtyName']
                else:
                    comment['authorSpecialtyName'] = ''
            
            comment['lang'] = comment_item['lang'] #就是figure id
            comment['text'] = comment_item['text']
            
            commentID_list.append(comment_item['_id'])
            
            if not self.db.if_comment_exist(comment_item['_id']):
                self.db.insertCommentByDict(comment, True)
               
        figure['comments'] = ','.join(commentID_list)
    
        if not self.db.if_figure_exist(homeData['imageID']):
            self.db.insertFigureByDict(figure, True)
            print '''成功保存一条'''


    def saveFigures(self, figureList):
        for figure in figureList:
            self.db.insertFigureByDict(figure, True)

    def saveComments(self, commentList):
        for comment in commentList:
            self.db.insertCommentByDict(comment, True)

    def runFigureThread(self, queue):
        for i in range(config.FIGURE1_THREAD_NUM):
            t = figureThread.FigureThread(self, queue)
            t.setDaemon(True)
            t.start()
            
        page_size = config.FIGURE1_PAGESIZE
        skip_num = int(config.FIGURE1_MAXSIZE / page_size) + 1
        
        for skip in range(skip_num):
            queue.put(skip)
        
        queue.join()
        
    def getImageIdUrl(self, image, queue):
        if image.has_key('imageSet'):
            imageSet = image['imageSet']
            for imageItem in imageSet:
                queue.put((imageItem['imageID'], imageItem['displaySrc']))
        else:
            queue.put((image['imageID'], image['displaySrc']))
        
    def runFigureImageThread(self, imageQueue):
        for i in range(config.FIGURE1_IMAGE_THREAD_NUM):
            t = figureImageThread.FigureImageThread(imageQueue)
            t.setDaemon(True)
            t.start()

        imageQueue.join()
        
            
if __name__ == '__main__':
    FetchDirector()
